﻿<%@ control language="C#" autoeventwireup="true" inherits="UserControl_ManagerArticle, App_Web_pmcgjoc4" %>
<%@ Import Namespace="DacSanMienTayOBJ" %>

<%@ Import Namespace="System.Collections.Generic" %>
<script type="text/javascript" src="Scripts/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckeditor/adapters/jquery.js"></script>
<script language="javascript" src="Scripts/jquery.alphanumeric.js"></script>
<script type="text/javascript" src="<%=Utility.modHost %>/Scripts/jquery.simplemodal.js"></script>

 <style>
  .imgareaselect-border1 {
	background: url('../images/border-v.gif') repeat-y left top;
}

.imgareaselect-border2 {
    background: url(../images/border-anim-h.gif) repeat-x left top;
}

.imgareaselect-border3 {
    background: url(../images/border-anim-v.gif) repeat-y right top;
}

.imgareaselect-border4 {
    background: url(../images/border-anim-h.gif) repeat-x left bottom;
}

.imgareaselect-border1, .imgareaselect-border2,
.imgareaselect-border3, .imgareaselect-border4 {
    filter: alpha(opacity=50);
	opacity: 0.5;
}

.imgareaselect-handle {
    background-color: #fff;
	border: solid 1px #000;
    filter: alpha(opacity=50);
	opacity: 0.5;
}

.imgareaselect-outer {
	background-color: #000;
    filter: alpha(opacity=50);
	opacity: 0.5;
}

.imgareaselect-selection {
}

#simplemodal-overlay {background-color:#000;}
#simplemodal-container {height:378px; width:619px; color:#bbb; background:url(../Images/bg_login.png) no-repeat center; padding:12px; position:relative;}
#simplemodal-container .simplemodal-data {position:relative;}
#simplemodal-container a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:0px; right:0px; cursor:pointer;}

#simplemodal_container_uploadimage {height:490px; width:700px; color:#bbb;background-color:White;border:5px;border-style:solid;border-color:#ce5c00; position:relative;margin-top:50px}
#simplemodal_container_uploadimage .simplemodal-data {position:relative;}
#simplemodal_container_uploadimage a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:-16px; right:-16px; cursor:pointer;}


  
  </style>

<div>
<input type="button" id="cmdShowAndHideArticle" value="Thêm mới" />
</div>

<div id="divArticleInfo" style="display:none">
<table id="tbArticleInfo">

<tr>
<td style="width:100px">
Tiêu đề bài viết
</td>
<td class="style1">
<input type="text" id="txtTitle" name="txtTitle"  style="width:300px" onfocus="ResetFieldArticle(1)" />
<label id="lbErrorTitle" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Chủ đề
</td>
<td class="style1">
<select id="cbTopic" name="cbTopic" onchange="ResetFieldArticle(3)">
<option value="0" >Chọn chủ đề</option>
<%
    List<TypeArticle> listTopic = TypeArticleBusiness.ListAllTypeArticleVisible(1, ConnectionType.AdminConnection);
    foreach (TypeArticle topic in listTopic)
    {
       
        %>
            <option value="<%= topic.TypeID %>"><%=topic.TypeName %></option>
            <%
    }
    
     %>
     </select>
     <label id="lbErrorTopic" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Tổng quan
</td>
<td class="style1">
<textarea id="txtSummary" name="txtSummary" cols="1" rows="3" style="width:800px" onfocus="ResetFieldArticle(2)" ></textarea> 
<label id="lbErrorSummary" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Từ khóa
</td>
<td class="style1" >
<textarea id="txtMetaKeyword" name="txtMetaKeyword" cols="1" rows="3" style="width:800px" ></textarea> 

</td>
</tr>


<tr>
<td style="width:100px">
Hình ảnh
</td>
<td class="style1">
<image src="" height="100px" width="100px" id="imgArticle" />
<asp:FileUpload ID="fileImage" runat="server" />

</td>
</tr>

<tr id="trDescription">
<td style="width:100px">
Nội dung
</td>
<td class="style1" >
<textarea style="width: 700px;margin-left:60px" id="txtDescription" rows="2" cols="1"  name="txtDescription"  ><span id="spanDescription"></span></textarea>
 <input name="txt_descPro"  id="txt_descPro" type="hidden" value="hello"/>
</td>
</tr>


<tr>
<td colspan="2">
<asp:Button ID="cmdAddArticle" runat="server" Text="Tạo mới" 
        OnClientClick="return CheckValidArt()" onclick="cmdAddArticle_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="cmdUpdateArticle" runat="server" Text="Cập nhật lại" 
        OnClientClick="return CheckValidArtUpdate()" 
        onclick="cmdUpdateArticle_Click"  />
</td>

</tr>
</table>

</div>

<div>
<span>
Danh Sách Bài Viết

</span>

<table>
<tr>
<td style="width:250px">
Tiêu đề
</td>
<td style="width:100px">
Hình ảnh
</td>

<td style="width:100px">
Ngày tạo
</td>

<td style="width:120px">
Trạng thái
</td>
<td style="width:150px">
Chỉnh sữa
</td>

<td style="width:150px">
Khóa/mở khóa
</td>
</tr>
<tbody id="bodyArticleList">
<%
    try
    {
        int recore = 250;
        int stt = 1;
        List<Article> listArt = ArticleBusiness.ListAllArticle(0, 50, ConnectionType.AdminConnection);
        if (listArt.Count > 0)
        {
            foreach (Article art in listArt)
            {

                string title = art.Title;
                string imagePath = Utility.modHost + "/" + art.ImageArticle;
                bool checkLocked = false;
               
               
                string statusAdv = "";
                if (art.IsVisible == 0)
                {
                    statusAdv = "Đang bị khóa";
                    checkLocked = false;
                }
                else if (art.IsVisible == 1)
                {
                    statusAdv = "Đang active";
                    checkLocked = true;
                }
                string description = art.ContentArticle;
                if (description != "")
                {
                  //  description = HttpUtility.HtmlDecode(description);
                }
                string summary = art.Summary;
                string metakey = art.Metakeyword;

                string linkArticle = Utility.userHost + "/bai-viet/" + art.ArticleID +"-"+Utility.sefWord(art.Title)+ ".html";
              
        %>
        <tr id="trArticle<%=stt %>">
        <td>
        <a href="<%=linkArticle %>" target="_blank" style="text-decoration:none">
        <%=title%>
        </a>
        </td>
    
        <td>
         <a href="<%=linkArticle %>" target="_blank" style="text-decoration:none">
       <image src="<%=imagePath %>" height="100px" width="100px" />
       </a>
        </td>
        <td>
        <%=art.CreateDate.ToShortDateString()%>
        </td>
        <td>
        <%=statusAdv%>
        </td>
        
    
       <td>
 
       <a href="#" style="color:Blue; cursor:pointer" rel="<%=title %>" rev="<%=summary %>" lang="<%=metakey %>" title="<%=title %>"  onclick="ShowInfoArticle('<%=art.ArticleID %>','<%=art.ImageArticle %>',this.title,this.rel,this.rev,this.lang,'<%=art.TypeID %>')"> Chỉnh sữa</a>
       </td>
       <td>
        <%
            if (checkLocked)
            {
                 %>
                    <input type="button" title="<%=art.ArticleID %>"  value="Khóa" onclick="LockAndUnlockArticle(this.title,'lock','<%=stt %>')" />
                <%
            }
            else
            {
             %>
                 <input type="button" title="<%=art.ArticleID %>"  value="Mở khóa" onclick="LockAndUnlockArticle(this.title,'unlock','<%=stt %>')" />
                <%
            }
                     %>
       </td>
       <td>
       <span style="cursor:pointer; color:blue" onclick="MoveArticle('<%=art.ArticleID %>','movetop')">Top</span>
       <span style="cursor:pointer; color:blue" onclick="MoveArticle('<%=art.ArticleID %>','movebottom')">bottom</span>
       </td>
        </tr>
        <%
            stt++;
    }
            
          %>
          <tr style="line-height:30px">
          <td>
          
          </td>
          </tr>
         <tr>
          <td colspan="7">
          Trang : 
         <%
             
             int totalRecore = ArticleBusiness.CountAllArticle(ConnectionType.AdminConnection);
             int numPage = totalRecore / recore;
             int balance = totalRecore % recore;
             if (balance > 0)
             {
                 numPage += 1;
             }
             for (int i = 1; i <= numPage; i++)
             {
             %>
         <a href="#" style="text-decoration:none" onclick="PaggingArticleList('<%=i %>')"><%=i %> &nbsp</a>

         <%
            }
                %>
         </td>

         </tr>
          
          <%  
        }
    }
    catch
    {

    }
     %>

</tbody>

</table>
</div>

  <div id="div_topinfor" class="simplemodal-data" style="display:none"></div>
<input type="hidden" id="hdArticleID" name="hdArticleID" />

<script type="text/javascript">


    $('#cmdShowAndHideArticle').click(function () {
        var valueButton = $('#cmdShowAndHideArticle').val();
        if (valueButton == 'Thêm mới') {
            $('#divArticleInfo').css("display", "inline");
            $('#cmdShowAndHideArticle').val('Đóng lại');
        }
        else if (valueButton == 'Đóng lại') {
            $('#divArticleInfo').css("display", "none");
            $('#cmdShowAndHideArticle').val('Thêm mới');

        }

    });

    //    $(document).ready(function () {
    //        $('#txtDescription').ckeditor(function () { /* callback code */ },
    //                {
    //                  
    //                    toolbar:
    //		            [
    //			            { name: 'styles', items: ['Styles', 'Format', 'Font', 'FontSize'] },
    //		                { name: 'clipboard', items: ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'] },
    //		                { name: 'editing', items: ['Find', 'Replace', '-', 'SelectAll', '-', 'Scayt'] },

    //                                '/',

    //		                { name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'] },
    //		                { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl'] },
    //		                { name: 'links', items: ['Link', 'Unlink', 'Anchor'] },
    //		                { name: 'tools', items: ['TextColor', 'BGColor', 'Maximize', '-', 'Preview'] },
    //		                { name: 'forms', items: ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'] },
    //		                { name: 'insert', items: ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe'] }
    //		            ]
    //                });


    //            });

    $(document).ready(function () {


        CKEDITOR.on('dialogDefinition', function (ev) {
            var dialogName = ev.data.name;
            var dialogDefinition = ev.data.definition;
            if (dialogName == 'link') {
                dialogDefinition.removeContents('advanced');
                dialogDefinition.removeContents('target');
            }
            if (dialogName == 'image') {
                dialogDefinition.removeContents('advanced');
                dialogDefinition.removeContents('Link');
            }
            if (dialogName == 'flash') {
                dialogDefinition.removeContents('advanced');
            }
        });
        CKEDITOR.replace('txtDescription',
            {
                fullPage: true,
                extraPlugins: 'tableresize,docprops,autogrow,uploadimage',
                autoGrow_maxHeight: 250,
                toolbar:
	            [
		            { name: 'styles', items: ['Font', 'FontSize'] },
	                { name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', '-', 'TextColor', 'BGColor'] },
	                { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'] },
	                { name: 'insert', items: ['Uploadimage', '-', 'Table', 'Smiley'] },
                   { name: 'links', items: ['Link', 'Unlink', 'Anchor'] },
	            ]
            });


    });

    function InsertImageContent() {

        ShowUploadImageContent();
    }

    function ShowUploadImageContent() {
        var rand_no = Math.random();
        var loading = '<img src="<%=Utility.modHost %>/Icons/loading.gif" alt="" style="margin-left:auto;margin-right:auto;margin-top:23%;margin-left:45%;width:40px;height:40px" />';
        $('#div_topinfor').html(loading);
        $('#div_topinfor').modal({
            opacity: 80,
            containerId: 'simplemodal_container_uploadimage',
            onOpen: function (dialog) {
                dialog.overlay.fadeIn('slow', function () {
                    dialog.container.slideDown('slow', function () {
                        dialog.data.fadeIn('slow');
                        $('#div_topinfor').load("<%=Utility.modHost %>/Proccess/ProccessShowButtonUploadArticle.aspx?_=" + rand_no);
                    });
                });
            },
            onClose: function (dialog) {
                dialog.data.fadeOut('slow', function () {
                    dialog.container.slideUp('slow', function () {
                        dialog.overlay.fadeOut('slow', function () {
                            $.modal.close();
                            $('#div_topinfor').html('');
                        });
                    });
                });
            }
        });
    }




    function CheckValidArt() {
        var title = $('#txtTitle').val();
        var summary = $('#txtSummary').val();
        var topicID = $('#cbTopic').val();
        // var image = $('#<%=fileImage.ClientID %>').val();
        //                var _text = $('#txtDescription').val();
        //                _text = $('#txtDescription').text(_text).html();

        //                $('#txt_descPro').val(_text);

        var checkRegullar = true;
        if (title == '') {
            checkRegullar = false;
            $('#lbErrorTitle').text("*");
        }
        if (summary == '') {
            checkRegullar = false;
            $('#lbErrorSummary').text("*");
        }
        if (topicID == '0') {
            checkRegullar = false;
            $('#lbErrorTopic').text("*");
        }

        if (checkRegullar == false) {
            return false;
        }
        else {
            return true;
        }
    }

    function ResetFieldArticle(id) {
        if (id == '1') {
            $('#lbErrorTitle').text(" ");
        }
        if (id == '2') {
            $('#lbErrorSummary').text(" ");
        }
        if (id == '3') {
            $('#lbErrorTopic').text(" ");
        }
    }

    function ShowInfoArticle(articleID, image, description,title, summary, metakey, topicID) {

        $('#hdArticleID').val(articleID);
        $('#divArticleInfo').css("display", "inline");
        $('#txtTitle').val(title);
      
        $('#txtSummary').val(summary);
        $('#txtMetaKeyword').val(metakey);
        $('#imgArticle').attr("src", image);
        $('#cbTopic option[value=' + topicID + ']').attr("selected", true);

        $.post("<%=Utility.modHost %>/Proccess/ProccessGetDescriptionArticle.aspx", { articleID: articleID }, function (data) {

            var oEditor = CKEDITOR.instances.txtDescription;
            oEditor.setData(data);

        });

        $('body,html').animate({ scrollTop: 0 }, 300);
    }

    function LockAndUnlockArticle(articleID, type, vt) {
        var trID = '#trArticle' + vt;
        var load = '<img src="<%=Utility.modHost %>/Images/loading.gif" id="imgLoad" alt="" style="margin-left:auto;margin-right:auto;margin-top:53%;margin-left:135%;width:40px;height:40px" />';
        $(trID).html(load);

        $.post("<%=Utility.modHost %>/Proccess/ProccessLockAndUnlockArticle.aspx", { articleID: articleID, type: type, vt: vt }, function (data) {
            $('#imgLoad').attr("src", "");
            if (data == "lockfaile") {
                alert('Không khóa được bài viết này. Vui lòng liên hệ admin');
            }
            else if (data == "unlockfaile") {
                alert('Không mở khóa được bài viết này. Vui lòng liên hệ admin');
            }
            else {
                document.getElementById('trArticle' + vt).innerHTML = data;
            }
        });
    }

    function CheckValidArtUpdate() {
        var title = $('#txtTitle').val();
        var summary = $('#txtSummary').val();
        var topicID = $('#cbTopic').val();
        // var image = $('#<%=fileImage.ClientID %>').val();
        //                var _text = $('#txtDescription').val();
        //                _text = $('#txtDescription').text(_text).html();

        //                $('#txt_descPro').val(_text);
        var checkRegullar = true;
        if (title == '') {
            checkRegullar = false;
            $('#lbErrorTitle').text("*");
        }
        if (summary == '') {
            checkRegullar = false;
            $('#lbErrorSummary').text("*");
        }
        if (topicID == '0') {
            checkRegullar = false;
            $('#lbErrorTopic').text("*");
        }

        if (checkRegullar == false) {
            return false;
        }
        else {
            return true;
        }
    }

    function MoveArticle(articleID, typeMove) {
        var load = '<img src="<%=Utility.modHost %>/Images/loading.gif" id="imgLoad" alt="" style="margin-left:auto;margin-right:auto;margin-top:53%;margin-left:135%;width:40px;height:40px" />';
        $('#bodyArticleList').html(load);
        $.ajax({
            type: "post",
            url: "<%=Utility.modHost %>/Proccess/ProccessUpdatePriorityArticle.aspx",
            data: { articleID: articleID, typeMove: typeMove },
            async: false,
            success: function (data) {
                document.getElementById('bodyArticleList').innerHTML = data;
            }
        });
    }

</script>

